<?php
/**
 * 第一步：实例化Weibo，并依次传入appid,回调地址,appkey
 * 第二步：调用WeiboAgree获取授权
 * 第三步：在回调页面->调用getAccessToken方法（该方法会将获取的token openId的值从录入$openid属性，output为ture时返回该信息）
 * 第四步：在回调页面->调用getUserInfo方法，获取用户的基本信息通过code
 */
class Weibo{
	private $client_id;
	private $redirect_uri;
	private $client_secret;
	private $openid;
	private $access_token;
	private $token_arr;

	/**
	 * @param int $client_id     应用的appid
	 * @param string $redirect_uri  成功授权后的回调地址
	 * @param  string $client_secret  应用的appkey
	 * @param string $state         client端的状态值。用于第三方应用防止CSRF攻击 默认状态值为test
	 */
	public function __construct($client_id,$redirect_uri,$client_secret){
		$this->client_id=$client_id;
		$this->redirect_uri=$redirect_uri;
		$this->client_secret=$client_secret;
	}
	
	/**
	 * 获取微博授权
	 * 说明"http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6"
	 */
	public function weiboAgree(){
		$client_id=$this->client_id;
		$redirect_uri=$this->redirect_uri;
		$url = 'https://api.weibo.com/oauth2/authorize?client_id='.$client_id.'&response_type=code&redirect_uri='.urlencode($redirect_uri);
		header('Location:'.$url);
	}

	/**
	 * 获取AccessToken的值
	 *
	 * $output 默认位false 返回true 设置为true时返回获取到的token信息（包括openid）
	 * @return array()包括access_token uid 和 tooken有效期
	 * 具体返回说明详见 “http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6”；
	 */
	public function getAccessToken($output=true){
		$post = 'client_id='.$this->client_id.'&client_secret='.$this->client_secret.'&grant_type=authorization_code&redirect_uri='.$this->redirect_uri.'&code='.$_GET['code'];
		$curl = curl_init();
		curl_setopt ( $curl, CURLOPT_URL, 'https://api.weibo.com/oauth2/access_token');
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ( $curl, CURLOPT_POST, TRUE );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post );
		$result = curl_exec($curl);
		if(curl_errno($curl)!=0){
			echo curl_errno($curl).'<br/>'.curl_error($curl);exit;
		}
		curl_close($curl);
		$result = json_decode($result, true);
		$this->openid		= $result['uid'];
		$this->access_token	= $result['access_token'];
		$this->token_arr	= $result;
		if ($output) {
			return $result;
		}
		return true;
	}

	/**
	*
	* @return   array 用户的微博信息
	*数组字段说明 "http://open.weibo.com/wiki/2/users/show"
	*/
	public function getUserInfo($outputToken=false){
		$this->getAccessToken();
		$url = 'https://api.weibo.com/2/users/show.json?access_token='.$this->access_token.'&uid='.$this->openid;
		$curl = curl_init();
		curl_setopt ( $curl, CURLOPT_URL, $url);
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($curl);
		if(curl_errno($curl)!=0){
			echo curl_errno($curl).'<br/>'.curl_error($curl);exit;
		}
		curl_close($curl);
		$info = json_decode($result, true);
		if ($outputToken) {
			$token = $this->token_arr;
			return compact('token', 'info');
		}
		return $info;
	}
	/**
	*转发一条微博
	* @return   0 成功
	*           -1 失败
	*           -2 token失效
	*           -3 重复发布
	*
	*/
	public function repost($arr){
			$post ='access_token='.$arr["token"].'&status='.$arr["content"].'&id='.$arr["id"].'&is_comment=2&rip='.$arr["ip"];
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, 'https://api.weibo.com/2/statuses/repost.json');
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($curl, CURLOPT_TIMEOUT, 20);
			curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36');
			curl_setopt ( $curl, CURLOPT_ENCODING, "" );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
			curl_setopt ( $curl, CURLOPT_HEADER, FALSE );
			curl_setopt ( $curl, CURLINFO_HEADER_OUT, TRUE );
			curl_setopt ( $curl, CURLOPT_POST, TRUE );
			curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post);
			$results = curl_exec($curl);
			echo curl_error($curl);
			curl_close($curl);
			$result = json_decode($results,true);
			if(isset($result["text"]) && isset($result['favorited'])){
				return 0;
			}elseif($result['error']=='invalid_access_token'||$result['error']=='expired_token'){
				return -2;
			}elseif($result['error']=='repeat content!'){
				return -3;
			}else{
				return -1;
			}
		}
}